#! /bin/sh

op=$1

echo_help()
{
    echo "Usage: $0 {create|rename|delete|connect|disconnect <point> <source>}"
}

validate_source()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

validate_point()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

connect()
{

    point=$1
    source=$2

    if [ ! -e "$point" ];then
        mkdir -p ${point}
    fi

    disk=`echo "$point" | awk -F'/' '{print $(NF)}'`
    if [ -z "$disk" ];then
      disk=`echo "$point" | awk -F'/' '{print $(NF-1)}'`
    fi

    status=`df | grep "${source}.*${point}"`
    if [ -n "$status" ];then
        echo "${source} -> ${point}"
        exit 0
    fi

    vg=`echo "${disk}_vg"`
    status=`lspv | grep $source | grep $vg`
    if [ -z "$status" ];then
        importvg -y $vg $source
        varyonvg $vg
    fi

    status=`lspv | grep $source | grep $vg`
    if [ -z "$status" ];then
        echo "FAILED: ${vg} is not exists"
        exit 0
    fi

    mount $point
    status=`df | grep "${point}"`
    if [ -z "$status" ];then
        echo "FAILED: mount ${point}"
    fi
    echo "/dev/${source} -> ${point}"
}

disconnect()
{
    point=$1
    source=$2

    disk=`echo "$point" | awk -F'/' '{print $(NF)}'`
    if [ -z "$disk" ];then
      disk=`echo "$point" | awk -F'/' '{print $(NF-1)}'`
    fi

    status=`df | grep "${point}"`
    if [ -z "$status" ];then
        echo "$point is not exists"
        exit 0
    fi

    umount -f ${point} >> /dev/null
    status=`df | grep "${point}"`
    if [ -n "$status" ];then
        echo "FAILED: umount -f ${point}"
        exit 1
    fi

    vg=`echo "${disk}_vg"`
    status=`lspv | grep $source | grep $vg`
    if [ -n "$status" ];then
        varyoffvg $vg
        exportvg $vg
    fi

    echo "OK"
}

rename()
{

  point=$1
  source=$2

  disk=`echo "$point" | awk -F'/' '{print $(NF)}'`
  if [ -z "$disk" ];then
    disk=`echo "$point" | awk -F'/' '{print $(NF-1)}'`
  fi

  vg=`lspv | grep hdisk4 | awk -F' ' '{print $3}'`
  vg_state=`lspv | grep hdisk4 | awk -F' ' '{print $4}'`
  if [ "$vg_state" = "active" ];then
      varyoffvg $vg
  fi
  exportvg $vg

  vg=`echo "${disk}_vg"`
  lv=`echo "${disk}_lv"`
  lg=`echo "${disk}_lg"`

  chdev -l $source -a pv=clear 2> /dev/null
  chdev -l $source -a pv=yes 2> /dev/null

  recreatevg -y $vg -Y $lv -L $point $source 2> /dev/null 1> /dev/null
  status=`lspv | grep $source | grep "$vg"`
  if [ -z "$status" ];then
    echo "FAILED: recreatevg $source"
    exit 1
  fi

  oldlv=`lsvg -l $vg | grep "jfs2 " | awk -F' ' '{print $1}'`
  oldlg=`lsvg -l $vg | grep "jfs2log" | awk -F' ' '{print $1}'`
  chlv -n $lv $oldlv
  chlv -n $lg $oldlg

  chfs -m $point -a log=/dev/$lg /dev/$lv
  fsck -p /dev/$lv

  varyoffvg $vg
  exportvg $vg
  echo "OK"
}

create()
{

  point=$1
  source=$2

  disk=`echo "$point" | awk -F'/' '{print $(NF)}'`
  if [ -z "$disk" ];then
    disk=`echo "$point" | awk -F'/' '{print $(NF-1)}'`
  fi

  vg=`echo "${disk}_vg"`
  lv=`echo "${disk}_lv"`

  chdev -l $source 2> /dev/null

  chpv -C $source
  mkvg -y $vg $source
  if [ "$?" != "0" ];then
      echo "failed: create vg"
      exit 1
  fi

  size=`lsvg $vg | grep "TOTAL PPs" | awk -F' ' '{ print $6}'`
  if [ -z "$size" ];then
      reducevg -dl $vg $source
      echo "failed: create volume size"
      exit 1
  fi
  size=$((size-3))

  mklv -t jfs2 -y $lv $vg $size
  if [ "$?" != "0" ];then
      reducevg -dl $vg $source
      echo "failed: create lv"
      exit 1
  fi

  crfs -v jfs2 -m $point -d $lv
  if [ "$?" != "0" ];then
      rmlv $lv << EOF
y
EOF
      varyoffvg $vg
      exportvg $vg
      echo "failed: format lv"
      exit 1
  fi

  mount $point
  if [ "$?" != "0" ];then
      rmlv $lv << EOF
y
EOF
      rmfs -r /dev/$lv
      varyoffvg $vg
      exportvg $vg

      echo "failed: mount fs"
      exit 1
  fi
}

delete()
{
    point=$1
    source=$2

    disk=`echo "$point" | awk -F'/' '{print $(NF)}'`
    if [ -z "$disk" ];then
      disk=`echo "$point" | awk -F'/' '{print $(NF-1)}'`
    fi
    vg=`echo "${disk}_vg"`

    status=`df | grep "${point}"`
    if [ -z "$status" ];then
        echo "$point is not exists"
        exit 0
    fi

    umount -f ${point} >> /dev/null
    if [ "$?" != "0" ];then
        echo "FAILED: umount -f ${point}"
        exit 1
    fi

    varyoffvg $vg
    exportvg $vg
    rmdev -dl $source
    echo "OK"
    rm -fr ${point} 2> /dev/null
}

case ${op} in
create)
    create_a=$2
    create_b=$3

    validate_point ${create_a}
    validate_source ${create_b}

    create ${create_a} ${create_b}
    ;;
connect)
    connect_a=$2
    connect_b=$3

    validate_point ${connect_a}
    validate_source ${connect_b}

    connect ${connect_a} ${connect_b}
    ;;
rename)
    rename_a=$2
    rename_b=$3

    validate_point ${rename_a}
    validate_source ${rename_b}

    rename ${rename_a} ${rename_b}
    ;;
disconnect)
    disconnect_a=$2
    disconnect_b=$3

    validate_point ${disconnect_a}
    validate_source ${disconnect_b}

    disconnect ${disconnect_a} ${disconnect_b}
    ;;
delete)
    delete_a=$2
    delete_b=$3

    validate_point ${delete_a}
    validate_source ${delete_b}

    delete ${delete_a} ${delete_b}
    ;;
*)
    echo_help
    exit 1
esac